class ManualPromise<T> {
  private _resolve: ((value: T) => void) | null = null;
  private _reject: ((reason?: any) => void) | null = null;

  constructor() {
    const executor = (resolve: (value: T) => void, reject: (reason?: any) => void) => {
      this._resolve = resolve;
      this._reject = reject;
    };

    this.promise = new Promise(executor);
  }

  public promise: Promise<T>;

  public resolve(value: T): void {
    if (this._resolve) {
      this._resolve(value);
    }
  }

  public reject(reason?: any): void {
    if (this._reject) {
      this._reject(reason);
    }
  }
}

export const manualPromise = new ManualPromise<any>();

// manualPromise.promise
//   .then((value) => {
//     console.log('Resolved:', value);
//   })
//   .catch((reason) => {
//     console.log('Rejected:', reason);
//   });

// // 手动控制 Promise 状态
// manualPromise.resolve(42); // 输出: Resolved: 42
// // 或 manualPromise.reject('Something went wrong'); // 输出: Rejected: Something went wrong
